/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lsi.sgb.gui;

import br.com.lsi.sgb.dao.AlunoDao;
import br.com.lsi.sgb.dao.ContatoDao;
import br.com.lsi.sgb.dao.UsuarioDao;
import br.com.lsi.sgb.model.Aluno;
import br.com.lsi.sgb.util.Contato;
import br.com.lsi.sgb.util.Usuario;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Livia
 */
public class AddAluno extends javax.swing.JDialog {

    /**
     * Creates new form AddAluno
     */
    private boolean flag;
    private Aluno aluno;
    private Usuario usuario;
    private Contato contato;

    public AddAluno(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setLocationRelativeTo(null);
        initComponents();
        this.flag = false;
    }

    public AddAluno(java.awt.Frame parent, boolean modal, Aluno aluno, Contato contato, Usuario usuario) {
        super(parent, modal);
        setLocationRelativeTo(this);
        initComponents();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
        this.aluno = aluno;
        this.contato = contato;
        this.usuario = usuario;
        campoNomeAluno.setText(aluno.getNome());
        campoMatricula.setText(aluno.getMatricula() + "");
        campoCpf.setText(aluno.getCpf() + "");
        campoRg.setText(aluno.getIdentidade() + "");
        String data = sdf.format(aluno.getDataNascimento().getTime());
        campoData.setText(data + "" );
        campoPeriodo.setText(aluno.getPeriodo() + "");
        campoCurso.setText(aluno.getCurso() + "");
        campoCelular.setText(contato.getCelular() + "");
        campoEmail.setText(contato.getEmail() + "");
        campoTelefone.setText(contato.getTelefone() + "");
        campoLogin.setText(usuario.getLogin() + "");
        campoSenha.setText(usuario.getSenha() + "");
        this.flag = true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        nomeAluno = new javax.swing.JLabel();
        campoNomeAluno = new javax.swing.JTextField();
        matricula = new javax.swing.JLabel();
        campoMatricula = new javax.swing.JTextField();
        cpf = new javax.swing.JLabel();
        campoCpf = new javax.swing.JTextField();
        rg = new javax.swing.JLabel();
        campoRg = new javax.swing.JTextField();
        dataNascimento = new javax.swing.JLabel();
        campoData = new javax.swing.JTextField();
        Curso = new javax.swing.JLabel();
        periodo = new javax.swing.JLabel();
        salvar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();
        campoPeriodo = new javax.swing.JTextField();
        campoCurso = new javax.swing.JTextField();
        email = new javax.swing.JLabel();
        campoEmail = new javax.swing.JTextField();
        celular = new javax.swing.JLabel();
        campoCelular = new javax.swing.JTextField();
        telefone = new javax.swing.JLabel();
        campoTelefone = new javax.swing.JTextField();
        login = new javax.swing.JLabel();
        campoLogin = new javax.swing.JTextField();
        senha = new javax.swing.JLabel();
        campoSenha = new javax.swing.JPasswordField();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        nomeAluno.setText("Nome: ");

        campoNomeAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoNomeAlunoActionPerformed(evt);
            }
        });

        matricula.setText("Matricula: ");

        cpf.setText("CPF:");

        rg.setText("RG: ");

        dataNascimento.setText("Data Nascimento: ");

        Curso.setText("Curso: ");

        periodo.setText("Periodo: ");

        salvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/lsi/sgb/imagens/1294_16x16.png"))); // NOI18N
        salvar.setText("Salvar");
        salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarActionPerformed(evt);
            }
        });

        cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/lsi/sgb/imagens/cancel.png"))); // NOI18N
        cancelar.setText("Cancelar ");
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });

        campoPeriodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoPeriodoActionPerformed(evt);
            }
        });

        email.setText("E-mai: ");

        celular.setText("Celular:");

        telefone.setText("Telefone");

        campoTelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoTelefoneActionPerformed(evt);
            }
        });

        login.setText("Login: ");

        senha.setText("Senha:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nomeAluno)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cpf)
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campoNomeAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(email)
                                .addGap(21, 21, 21)
                                .addComponent(campoEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(periodo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(campoPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(celular)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(campoCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(salvar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(cancelar)
                                        .addGap(14, 14, 14))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(Curso)
                                            .addGap(18, 18, 18)
                                            .addComponent(campoCurso))
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(matricula)
                                                .addComponent(rg))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(campoMatricula, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                                                .addComponent(campoRg)))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(dataNascimento)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(campoData, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(telefone)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(campoSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                                    .addComponent(campoTelefone))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(login)
                        .addGap(18, 18, 18)
                        .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(senha)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nomeAluno)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(email)
                            .addComponent(campoEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(periodo)
                            .addComponent(campoPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoNomeAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(matricula)
                            .addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rg)
                            .addComponent(campoRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cpf))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dataNascimento)
                            .addComponent(campoData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Curso)
                            .addComponent(campoCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 8, Short.MAX_VALUE)))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(celular)
                    .addComponent(campoCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(telefone)
                    .addComponent(campoTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(login)
                    .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(senha)
                    .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelar)
                    .addComponent(salvar))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoNomeAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoNomeAlunoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoNomeAlunoActionPerformed

    private void salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarActionPerformed
        AlunoDao dao = new AlunoDao();
        Aluno a = new Aluno();
        ContatoDao contatodao = new ContatoDao();
        UsuarioDao usuariodao = new UsuarioDao(); 
        Contato c = new Contato(); 
        Usuario u = new Usuario();
        if (!flag) {
                a.setNome(campoNomeAluno.getText());
                a.setMatricula(Integer.valueOf(campoMatricula.getText()));
                a.setCpf(Integer.valueOf(campoCpf.getText()));
                a.setCurso(campoCurso.getText());
                a.setIdentidade(Integer.valueOf(campoRg.getText()));
                a.setPeriodo(Integer.valueOf(campoPeriodo.getText()));
                c.setCelular(campoCelular.getText());
                c.setEmail(campoEmail.getText());
                c.setTelefone(campoTelefone.getText());
                u.setLogin(campoLogin.getText());
                u.setSenha(campoSenha.getText());
                contatodao.addContato(c);
                usuariodao.addUsuario(u);
                a.setContato(c);
                a.setUsuario(u);
                
                try{
                    String data = campoData.getText(); 
                    SimpleDateFormat sdf; 
                    Date date;
                    sdf = new SimpleDateFormat("dd/MM/yyyy"); 
                    date = (Date)sdf.parse(data);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    a.setDataNascimento(cal);
                }catch (ParseException e){
                    e.printStackTrace();
                }
                
                dao.addAluno(a);
                JOptionPane.showMessageDialog(null, "Aluno cadastrado com sucesso.", "Ok", JOptionPane.INFORMATION_MESSAGE);
                dispose();
           
        } else {
            aluno.setNome(campoNomeAluno.getText());
            aluno.setMatricula(Integer.valueOf(campoMatricula.getText()));
            aluno.setCpf(Integer.valueOf(campoCpf.getText()));
            aluno.setCurso(campoCurso.getText());
            aluno.setIdentidade(Integer.valueOf(campoRg.getText()));
            aluno.setPeriodo(Integer.valueOf(campoPeriodo.getText()));
            contato.setCelular(campoCelular.getText());
            contato.setEmail(campoEmail.getText());
            contato.setTelefone(campoTelefone.getText());
            usuario.setLogin(campoLogin.getText());
            usuario.setSenha(campoSenha.getText());
            contatodao.atualizarContato(contato);
            usuariodao.atualizarUsuario(usuario);
            aluno.setContato(contato);
            aluno.setUsuario(usuario);
      
            try{
                    String data = campoData.getText(); 
                    SimpleDateFormat sdf; 
                    Date date;
                    sdf = new SimpleDateFormat("dd/MM/yyyy"); 
                    date = (Date)sdf.parse(data);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    a.setDataNascimento(cal);
                }catch (ParseException e){
                    e.printStackTrace();
                }
            dao.atualizarAluno(aluno);
            JOptionPane.showMessageDialog(null, "Aluno editado com sucesso.", "Ok", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        }
    }//GEN-LAST:event_salvarActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
        dispose();
    }//GEN-LAST:event_cancelarActionPerformed

    private void campoPeriodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoPeriodoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoPeriodoActionPerformed

    private void campoTelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoTelefoneActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoTelefoneActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddAluno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddAluno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddAluno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddAluno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddAluno dialog = new AddAluno(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Curso;
    private javax.swing.JTextField campoCelular;
    private javax.swing.JTextField campoCpf;
    private javax.swing.JTextField campoCurso;
    private javax.swing.JTextField campoData;
    private javax.swing.JTextField campoEmail;
    private javax.swing.JTextField campoLogin;
    private javax.swing.JTextField campoMatricula;
    private javax.swing.JTextField campoNomeAluno;
    private javax.swing.JTextField campoPeriodo;
    private javax.swing.JTextField campoRg;
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JTextField campoTelefone;
    private javax.swing.JButton cancelar;
    private javax.swing.JLabel celular;
    private javax.swing.JLabel cpf;
    private javax.swing.JLabel dataNascimento;
    private javax.swing.JLabel email;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel login;
    private javax.swing.JLabel matricula;
    private javax.swing.JLabel nomeAluno;
    private javax.swing.JLabel periodo;
    private javax.swing.JLabel rg;
    private javax.swing.JButton salvar;
    private javax.swing.JLabel senha;
    private javax.swing.JLabel telefone;
    // End of variables declaration//GEN-END:variables
}
